package org.vectrics.mail;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;
import org.vectrics.common.db.Auditable;
import org.vectrics.common.db.RecordObject;
import org.vectrics.common.util.FileUtil;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;

public class FileEmailTemplate implements RecordObject, Auditable {
	private Logger log = Logger.getLogger(this.getClass().getName());
	private Long id = null;
	private String fileName = null;
	private boolean voided = false;
	private String name = null;
	private String subject = null;
	private String description = null;
	private Date lastModifiedDate = null;
	private Person lastModifiedBy = null;
	private Person createdBy = null;
	private Date createdDate = null;
	private Domain domain = null;
	private FileEmailTemplateCategory category = null;

	public boolean logEntityChanges() {
		return(true);
	}
	
	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Person getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(Person createdBy) {
		this.createdBy = createdBy;
	}

	public FileEmailTemplateCategory getCategory() {
		return category;
	}



	public void setCategory(FileEmailTemplateCategory category) {
		this.category = category;
	}



	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Date getLastModifiedDate() {
		return lastModifiedDate;
	}

	public void setLastModifiedDate(Date lastModifiedDate) {
		this.lastModifiedDate = lastModifiedDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean getVoided() {
		return voided;
	}

	public void setVoided(boolean voided) {
		this.voided = voided;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public Person getLastModifiedBy() {
		return lastModifiedBy;
	}

	public void setLastModifiedBy(Person lastModifiedBy) {
		this.lastModifiedBy = lastModifiedBy;
	}



	public Domain getDomain() {
		return domain;
	}



	public void setDomain(Domain domain) {
		this.domain = domain;
	}



	public String getDescription() {
		return description;
	}



	public void setDescription(String description) {
		this.description = description;
	}

	
	public String getText() {
		FileTemplateManager templateManager = new FileTemplateManager();
		String filePath = null;

		if (this.getFileName() == null) {
			filePath = templateManager.getTemplateFilePath(this.getCategory(), this.getId());
	    } else {
			filePath = templateManager.getCategoryDirectory(category) + "/" + this.getFileName();
		}
		
		log.info("Template content in file: " + filePath);
		
		String text = null;
		File file = new File(filePath);
		if (file.exists()) {
			text = FileUtil.getFileContents(filePath);
		} else {
			log.error("Email template file does not exist: " + filePath);
		}
		
		return(text);
	}
	
	public String getSubject() {
		return subject;
	}



	public void setSubject(String subject) {
		this.subject = subject;
	}



	public String toString() {
		return("{name=" + this.name + "|fileName=" + this.fileName + "|description=" + description + "}");
	}
}
